/**
 * http 请求拦截器
 */

import { STORAGE_USERTOKEN_KEY } from '@/consts';
import { Message } from '@arco-design/web-react';
import { omit } from 'lodash';
import { IInterceptor } from "./type";

/**
 * 内置的拦截器
 */
export const httpInterceptor: IInterceptor[] = [
    {
        request: (config) => {
            const tokenInfo = localStorage.getItem(STORAGE_USERTOKEN_KEY);
            // h5 页面需要的token
            const h5token = localStorage.getItem('h5token') || '';
            config.headers!.token = h5token
            let token: string;
            if (tokenInfo) {
                token = JSON.parse(tokenInfo).token;
            }
            if (token!) {
                config.headers!.loginToken = token;
            }
            return config;
        },
        response: {
            onSuccess({ data }) {
                if (data.result === 0) return data;
                Message.error(data.errMsg || '未知错误');
                return Promise.reject(data.errMsg);
            },
            onErr(err: any) {
                const msg = err.response.data.errMsg || err.message || '未知错误';
                Message.error(msg);
                return Promise.reject(err.message)
            }
        }
    }
];